package com.xinz.server;

import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xinz.srmi.Session;
import com.xinz.shared.exception.NetworkException;

public class ClientPool {
	
	private static final Logger log = LoggerFactory.getLogger(ClientPool.class);

	private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<String, Session>();
	
	public static void addClient(String key, Session session) {
		sessionPool.put(key, session);
	}
	
	public static Session getSession(String key) {
		log.info("session key:" + key);
		return sessionPool.get(key);
	}
	
	public static void removeSession(String key) {
		sessionPool.remove(key);
	}
	
	public static Session getSelfSession(String key) {
		log.info("session key:" + key);
		Session session = sessionPool.get(key);
		if(session == null){
			log.error("session is null,key:" + key);
			throw new NetworkException("网络错误，请重新登录");
		}
		return session;
	}
	
	public static Session getTargetSession(String key) {
		log.info("session key:" + key);
		Session session = sessionPool.get(key);
		if(session == null){
			log.error("session is null,key:" + key);
		}
		return session;
	}
}
